<%@ page language="java"  pageEncoding="utf-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<script type="text/javascript" charset="utf-8">
	$(function() {
		editRow=undefined;
		$('#admin_kcgl_datagrid').datagrid({
			url : ' ${pageContext.request.contextPath}/inventoryAction!datagrid.action ',
			title : '',
			iconCls : 'icon-save',
			pagination : true,
			pageSize : 5,
			pageList : [ 5, 10, 15, 20 ],
			fit : true,
			fitColumns : true,
			nowarp : false,
			border : false,
			idField : 'id',
			checkOnSelect:true,
			selectOnCheck:true,
			columns : [ [ {
				field : 'id',
				title : '编号',
				width : 100,
				checkbox:true
			}, {
				field : 'code',
				title : '商品号',
				width : 100
			},{
				field : 'name',
				title : '商品名称',
				width : 100
			}, {
				field : 'unit',
				title : '单位',
				width : 100
			}, {
				field : 'originalsum',
				title : '原始数量',
				width : 100,
				sortable :true,
				editor:{
					type:'numberbox',
					options:{
						 required:true,
						 missingMessage:'该项必须输入正整数',
						}
			           }
			}, {
				field : 'amount',
				title : '现有库存',
				width : 100,
				sortable : true,
				editor:{
					type:'numberbox',
					options:{
						 required:true,
						 missingMessage:'该项必须输入正整数',
						}
			           }
			} ] ],
			toolbar:[{
				text:'修改',
				iconCls:'icon-edit',
				handler:function(){
					var rows=$('#admin_kcgl_datagrid').datagrid('getChecked');
					if(rows.length==1){
						if(editRow!=undefined){
							$('#admin_kcgl_datagrid').datagrid('endEdit',editRow);	
						}
						if(editRow==undefined){
							var index= $('##admin_kcgl_datagrid').datagrid('getRowIndex',rows[0]);
						    $('#admin_kcgl_datagrid').datagrid('beginEdit',index);
						    editRow=index;
						    $('#admin_kcgl_datagrid').datagrid('unselectAll');
						}
					}
					if(rows.length>1){
						$.messager.alert('警告','同一时间只能修改一条记录!','warning');
					}if(rows.length<1){
						$.messager.alert('警告','请选择要修改的记录!','warning');
					}
				}
			},'-',{
					text:'保存',
					iconCls:'icon-save',
					handler:function(){
						$('#admin_kcgl_datagrid').datagrid('endEdit',editRow);
					}
			},'-',{
				text:'取消编辑',
				iconCls:'icon-undo',
				handler:function(){
					editRow=undefined;
					$('#admin_kcgl_datagrid').datagrid('rejectChanges');
					$('#admin_kcgl_datagrid').datagrid('unselectAll');
				}
			},'-',{
				text : '刷新',
				iconCls : 'icon-reload',
				handler : function(){
				  $('#admin_kcgl_datagrid').datagrid('reload');
			}}],
			onAfterEdit:function(rowIndex,rowData,changes){
				var updated=$('#admin_kcgl_datagrid').datagrid('getChanges','updated');
				if (updated.length < 1) {
					editRow = undefined;
					$('#admin_kcgl_datagrid').datagrid('unselectAll');
					return;
				}
				var url=' ';
				if(updated.length>0){
					url='inventoryAction!editor.action';
				}
				$.ajax({
					url:url,
					data:rowData,
					dataType:'json',
					success:function(r){
						if(r&&r.success){
							$('#admin_kcgl_datagrid').datagrid('acceptChanges');
							$('#admin_kcgl_datagrid').datagrid('load');
						    $.messager.show({
						    	title:'成功',
						    	msg:r.msg
						    	});
						}else{
							$('#admin_kcgl_datagrid').datagrid('rejectChanges');
						    $.messager.alert('错误',r.msg,'error');
						}
						editRow=undefined;
						$('#admin_kcgl_datagrid').datagrid('unselectAll');
					}
				});
			},
			onDblClickRow:function(rowIndex,rowData){
				if(editRow!=undefined){
					$('#admin_kcgl_datagrid').datagrid('endEdit',editRow);	
				}
				if(editRow==undefined){
				    $('#admin_kcgl_datagrid').datagrid('beginEdit',rowIndex);
				    editRow=rowIndex;
				    $('#admin_kcgl_datagrid').datagrid('unselectAll');
				}
			} 	
		});
		$('#admin_kcgl_searchbox').searchbox({
		    searcher:function(value,name){
		    	searchFun();
		    },
		    menu:'#mm',
		    prompt:'可进行模糊查询'
		});
	});
	function searchFun() {
		$('#admin_kcgl_datagrid').datagrid('load',serializeObject($('#admin_kcgl_searchForm')));
	}
	function clearFun() {
		$('#admin_kcgl_datagrid').datagrid('load', {});
	}
</script>
<div id="admin_kcgl_layout" class="easyui-layout" data-options="border:false,fit:true">
<div region="north" data-options="border:false,title:'过滤信息'" style="height:75px;overflow:hidden;">
<form id="admin_kcgl_searchForm" >
<div style="float:right;margin:5px;"><input id="admin_kcgl_searchbox" style="width:300px;"></input><a href="#" class="easyui-linkbutton" data-options="iconCls:'icon-undo',plain:true" onclick="clearFun();">回显所有</a></div>
</form>
<div id="mm" style="width:120px">
    <div data-options="name:'code',iconCls:'icon-ok'">商品号</div>
    <div data-options="name:'name',iconCls:'icon-ok'">名称</div>
</div>
</div>
<div region="center" data-options="border:false">
<table id="admin_kcgl_datagrid"></table>
</div>
</div>


